package sample;


import com.alibaba.fastjson.JSONObject;
import dorkbox.systemTray.Menu;
import dorkbox.systemTray.MenuItem;
import dorkbox.systemTray.SystemTray;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import utils.fileHandler.FileHandler;
import utils.groupsInfoHandler.GroupsInfoHandler;
import utils.wallpaperCtrl.WallpaperCtrlUKUILinux;
import utils.wallpaperHandler.WallpaperHandler;
import wallpaperSetting.WallpaperSettingCtrl;
import webView.WebViewCtrl;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;


public class Main extends Application {
    public static JSONObject jsonObjectStatic;
    public static Scene sceneStatic;
    public static Stage primaryStageStatic;
    public ActionListener callbackGray;

    public static Menu groupsInfoMenu;
    public static Menu systemTrayMenu;

    public static void flash(){
        systemTrayMenu.remove(groupsInfoMenu);
        groupsInfoMenu=new Menu("壁纸播放组别");
        for (String s : GroupsInfoHandler.getGroupsName()) {
            groupsInfoMenu.add(new MenuItem(s, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    WallpaperCtrlUKUILinux.setWallpaperPlayGroup(GroupsInfoHandler.getGroupInfo(s));
                }
            }));
        }
        systemTrayMenu.add(groupsInfoMenu,0);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Platform.setImplicitExit(false);
        SystemTray systemTray = SystemTray.get("SystemTray");
        if (systemTray == null) {
            throw new RuntimeException("Unable to load SystemTray!");
        }
        systemTray.setImage(FileHandler.getRootPath() + "wallpaper/testIcon1.png");
        systemTray.setTooltip("画屏");
        systemTray.setStatus("运行中");

        systemTrayMenu = systemTray.getMenu();

        groupsInfoMenu = new Menu("壁纸播放组别");
        for (String s : GroupsInfoHandler.getGroupsName()) {
            groupsInfoMenu.add(new MenuItem(s, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    WallpaperCtrlUKUILinux.setWallpaperPlayGroup(GroupsInfoHandler.getGroupInfo(s));
                }
            }));
        }
        systemTrayMenu.add(groupsInfoMenu);

        systemTrayMenu.add(new MenuItem("下一张壁纸", new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (WallpaperCtrlUKUILinux.wallpaperPlay == null) {
                } else {
                    WallpaperCtrlUKUILinux.wallpaperPlay.playNext(FileHandler.getTransparency(), FileHandler.getAudio());
                }
            }
        }));

        systemTrayMenu.add(new MenuItem("设置", new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        popUpWindow(WallpaperSettingCtrl.class, "wallpaperSetting");
                    }
                });
            }
        }));

        systemTrayMenu.add(new MenuItem("帮助", new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        popUpWindow(WebViewCtrl.class, "webView");
                    }
                });
            }
        }));

        systemTrayMenu.add(new MenuItem("打开界面", new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Platform.setImplicitExit(false);
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        primaryStageStatic.show();
                    }
                });
            }
        }));

        systemTrayMenu.add(new MenuItem("关闭界面", new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Platform.setImplicitExit(false);
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        primaryStageStatic.hide();
                    }
                });
            }
        }));

        systemTrayMenu.add(new MenuItem("彻底退出", new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                WallpaperHandler.save();
                FileHandler.save();
                GroupsInfoHandler.save();
                WallpaperCtrlUKUILinux.exitLiveWallpaperStatic();
                systemTray.shutdown();
                Platform.setImplicitExit(true);
                Platform.exit();
                System.exit(0);
            }
        }));

        primaryStageStatic = primaryStage;
        CantSee cantsee = new CantSee();
        URL location = getClass().getResource("/wallpaper.fxml");
        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(location);
        fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
        Parent root = fxmlLoader.load();
        root.getStylesheets().add(getClass().getResource("/wallpaper_star.css").toExternalForm());
        /*root.getStylesheets().add(getClass().getResource("/wallpaper.css").toExternalForm());*/
        Scene scene = new Scene(root);
        sceneStatic = scene;
        primaryStage.setTitle("画屏");
        primaryStage.setScene(scene);
        setPrimaryStageHW(970, 2010);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    public static void setPrimaryStageHW(double height, double width) {
        primaryStageStatic.setWidth(width);
        primaryStageStatic.setHeight(height);
    }

    public static void setScene(Class cl, String fxml) {
        try {
            sceneStatic = new Scene(new FXMLLoader(cl.getResource(fxml + ".fxml")).load());
            primaryStageStatic.setScene(sceneStatic);
        } catch (IOException e) {
            System.out.println("未找到" + fxml + ".fxml");
            e.printStackTrace();
        }
    }//切换窗口

    public static void popUpWindow(Class cl, String fxml) {
        Stage stage = new Stage();
        Scene scene = null;
        URL resourceCss = cl.getClassLoader().getResource(fxml + ".css");
        try {
            scene = new Scene(new FXMLLoader(cl.getResource(fxml + ".fxml")).load());
        } catch (IOException e) {
            System.out.println("未找到" + fxml + ".fxml");
            e.printStackTrace();
        }
        if (resourceCss!=null) {
            scene.getStylesheets().add(resourceCss.toExternalForm());
        }
        stage.setScene(scene);
        stage.show();
    }
}
